package com.example.longimageView.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Scroller;

/**
 * @auth: njb
 * @date: 2022/12/27 15:35
 * @desc:
 */
public class ScrollCustomView extends View {
    private Scroller mScroller;

    SizeChangeListener listener;

    final int DURATION = 100;

    final int HEIGHT = 100;

    public SizeChangeListener getListener() {
        return listener;
    }


    public void setListener(SizeChangeListener listener) {
        this.listener = listener;
    }


    public ScrollCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    void init(Context context) {
        mScroller = new Scroller(context);
    }


    public void scrollUp(int desX, int ms) {
        int startX = getScrollX();
        int startY = getScrollY();
        mScroller.startScroll(0, startY, desX, HEIGHT, ms);
        invalidate();
    }


    public void scrollDown(int desX, int ms){
        //获取View的内容当前已经偏移的坐标值
        int startX = getScrollX();
        int startY = getScrollY();
        //startScroll(x起始坐标，y起始坐标，x方向偏移值，y方向偏移值，滚动时长)
        mScroller.startScroll(startX, startY, desX-startX, desX, ms);
        //触发View的重新绘制
        invalidate();
    }




    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            int scrollY = mScroller.getCurrY();
            scrollTo(mScroller.getCurrX(), scrollY);
            postInvalidate();
        }

    }

    public interface SizeChangeListener {
        void onShowBottom(int height);
    }
}
